package com.dingup.paike.controller.schoolCampus;

import com.dingup.api.base.HttpResContent;
import com.dingup.api.enums.StudentCampusEnum;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/10/12.
 */
@Controller
@RequestMapping("/school/campus")
public class SchoolCampusController {

    @RequestMapping(value = "/get_all_school_campus.do", method = RequestMethod.POST)
    public void getAllSchoolCampus(HttpServletRequest request,
                               HttpServletResponse response) {
        HttpResContent<List<Map<String,Object>>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取全部校区");
        try {
            List<StudentCampusEnum> campus = StudentCampusEnum.getAll();
            List<Map<String,Object>> res = new ArrayList<>();
            campus.stream().forEach(
                    c->{
                        Map<String,Object> map = new HashMap<String, Object>();
                        map.put("id",c.getId());
                        map.put("name",c.getName());
                        res.add(map);
                    }
            );
            saveSuccessMsgAndData(hrc, res);
        }  catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
